<?php
/*
Plugin Name: JayBo Comments Widget
Plugin URI: http://www.jaybowalkin.com/categories/plugins
Description: Similar to the Recent Comments Widget, however allows excluding pingbacks
ategories
Author: Bryan Clark
Version: 0.1
Author URI: http://www.jaybowalkin.com/

*/ 

function jaybo_widget_recent_comments($args) {
	global $wpdb, $comments, $comment;
	extract($args, EXTR_SKIP);
	$options = get_option('jaybo_widget_recent_comments');
	$title = empty($options['title']) ? __('Recent Comments') : $options['title'];
	if ( !$number = (int) $options['number'] )
		$number = 5;
	else if ( $number < 1 )
		$number = 1;
	else if ( $number > 15 )
		$number = 15;
	$pingbacks = $options['pingbacks'] ? "" : " AND comment_type != 'pingback' ";

	if ( !$comments = wp_cache_get( 'jaybo_recent_comments', 'widget' ) ) {
		$comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID, comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' $pingbacks ORDER BY comment_date_gmt DESC LIMIT $number");
		wp_cache_add( 'jaybo_recent_comments', $comments, 'widget' );
	}
	if ( $comments ) :

		echo $before_widget;
		echo $before_title . $title . $after_title;
?>
			<div class="last-comment"><?php if (function_exists('jaybo_recent_comments_widgets_time_since')) { if (false /* date_default_timezone_set("GMT")*/) echo jaybo_recent_comments_widgets_time_since(abs(strtotime($comments[0]->comment_date_gmt) . " GMT"), time()) . " ago"; } else { echo $comments[0]->comment_date_gmt; } ?></div>
			<ul id="recentcomments"><?php
			foreach ($comments as $comment) :
			echo  '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
			endforeach; ?>
			</ul>
		<?php echo $after_widget;
	endif;
}

function jaybo_delete_recent_comments_cache() {
	wp_cache_delete( 'jaybo_recent_comments', 'widget' );
}
add_action( 'comment_post', 'jaybo_delete_recent_comments_cache' );
add_action( 'wp_set_comment_status', 'jaybo_delete_recent_comments_cache' );

function jaybo_widget_recent_comments_control() {
	$options = $newoptions = get_option('jaybo_widget_recent_comments');
	if ( $_POST["jaybo-recent-comments-submit"] ) {
		$newoptions['title'] = strip_tags(stripslashes($_POST["jaybo-recent-comments-title"]));
		$newoptions['number'] = (int) $_POST["jaybo-recent-comments-number"];
		$newoptions['pingbacks'] = isset($_POST['jaybo-recent-comments-pingbacks']);
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('jaybo_widget_recent_comments', $options);
		jaybo_delete_recent_comments_cache();
	}
	$title = attribute_escape($options['title']);
	if ( !$number = (int) $options['number'] )
		$number = 5;
	$pingbacks = $options['pingbacks'] ? 'checked="checked"' : '';
?>
			<p><label for="jaybo-recent-comments-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="jaybo-recent-comments-title" name="jaybo-recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p>
			<p><label for="jaybo-recent-comments-number"><?php _e('Number of comments to show:'); ?> <input style="width: 25px; text-align: center;" id="jaybo-recent-comments-number" name="jaybo-recent-comments-number" type="text" value="<?php echo $number; ?>" /></label> <?php _e('(at most 15)'); ?></p>
			<p style="text-align:right;margin-right:40px;"><label for="jaybo-recent-comments-pingbacks" style="text-align:right;"><?php _e('Show pingbacks with Comments'); ?> <input class="checkbox" type="checkbox" <?php echo $pingbacks; ?> id="jaybo-recent-comments-pingbacks" name="jaybo-recent-comments-pingbacks" /></label></p>
			<input type="hidden" id="jaybo-recent-comments-submit" name="jaybo-recent-comments-submit" value="1" />
<?php
}

function jaybo_widget_recent_comments_style() {
?>
<style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
<?php
}

function jaybo_recent_comments_widgets_init() {
	$dims = array('width' => 320, 'height' => 120);
	$class = array('classname' => 'jaybo_widget_recent_comments');
	wp_register_sidebar_widget('jaybo-recent-comments', __('JayBo Comments'), 'jaybo_widget_recent_comments', $class);
	wp_register_widget_control('jaybo-recent-comments', __('JayBo Comments'), 'jaybo_widget_recent_comments_control', $dims);
	
	if ( is_active_widget('jaybo_widget_recent_comments') )
		add_action('wp_head', 'jaybo_widget_recent_comments_style');

	do_action('widgets_init');
}

add_action('init', 'jaybo_recent_comments_widgets_init', 1);

/* ==================== HELPER FUNCTIONS =============================== */

/* 
Function Name: Time Since 
Function URI: http://binarybonsai.com/wordpress/timesince
Description: Tells the time between the entry being posted and the comment being made.
Author: Michael Heilemann & Dunstan Orchard
Author URI: http://binarybonsai.com
*/

function jaybo_recent_comments_widgets_time_since($older_date, $newer_date = false)
{
	// array of time period chunks
	$chunks = array(
	array(60 * 60 * 24 * 365 , 'year'),
	array(60 * 60 * 24 * 30 , 'month'),
	array(60 * 60 * 24 * 7, 'week'),
	array(60 * 60 * 24 , 'day'),
	array(60 * 60 , 'hour'),
	array(60 , 'minute'),
	);
	
	// $newer_date will equal false if we want to know the time elapsed between a date and the current time
	// $newer_date will have a value if we want to work out time elapsed between two known dates
	$newer_date = ($newer_date == false) ? (time()+(60*60*get_settings("gmt_offset"))) : $newer_date;
	
	// difference in seconds
	$since = $newer_date - $older_date;
	
	// we only want to output two chunks of time here, eg:
	// x years, xx months
	// x days, xx hours
	// so there's only two bits of calculation below:

	// step one: the first chunk
	for ($i = 0, $j = count($chunks); $i < $j; $i++)
		{
		$seconds = $chunks[$i][0];
		$name = $chunks[$i][1];

		// finding the biggest chunk (if the chunk fits, break)
		if (($count = floor($since / $seconds)) != 0)
			{
			break;
			}
		}

	// set output var
	$output = ($count == 1) ? '1 '.$name : "$count {$name}s";

	// step two: the second chunk
	if ($i + 1 < $j)
		{
		$seconds2 = $chunks[$i + 1][0];
		$name2 = $chunks[$i + 1][1];
		
		if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0)
			{
			// add to output var
			$output .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
			}
		}
	
	return $output;
}

